//
//  GlobalFlavor.m
//  Menu
//
//  Created by trimad on 11-11-22.
//  Copyright 2011 Trimad Group. All rights reserved.
//

#import "GlobalFlavor.h"
#import "ContextManager.h"

@implementation GlobalFlavor

@synthesize tableData;
@synthesize keys;
@synthesize comments;
@synthesize tableView;

 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	NSArray *tempArray1=[[NSArray alloc] initWithObjects: @"微辣",@"中辣",@"重辣",nil];
	
	NSArray *tempArray2=[[NSArray alloc] initWithObjects: @"油腻",@"清淡",@"爽口",nil];
	NSArray *tempArray3=[[NSArray alloc] initWithObjects: @"大火",@"中火",@"小火",nil];
	NSDictionary *tempDic=[[NSDictionary alloc] initWithObjectsAndKeys:tempArray1,@"1",tempArray2,@"2",tempArray3,@"3",nil];
	NSLog(@"tempDic is: %@ ",tempDic);
	self.keys=[tempDic allKeys];
	self.tableData=tempDic;
	
	ContextManager *contextMgr=[ContextManager getInstance];
	comments.text=contextMgr.globalOption;
	
	[self.tableView setBackgroundColor:[UIColor clearColor]];

}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[tableData release];
	[comments release];
    [super dealloc];
}

#pragma mark -
#pragma mark Table View Data Source Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	NSString *key=[self.keys objectAtIndex:section];
	NSArray *array=[self.tableData objectForKey:key];
	return [array count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return [self.keys count];
}

- (UITableViewCell *)tableView:(UITableView *)uiTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
	UITableViewCell *cell = [uiTableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle
									  reuseIdentifier:SimpleTableIdentifier] autorelease];
	}
	
	NSUInteger section1=[indexPath section];
	NSUInteger row1=[indexPath row];
	
	NSString *key=[self.keys objectAtIndex:section1];
	NSArray *array=[tableData objectForKey:key];
	NSString *option=[[array objectAtIndex:row1] stringByAppendingString:@"      "];
	cell.textLabel.text=option;
	return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
	return @"";
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	NSLog(@"didSelectRowAtIndexPath");
	NSUInteger section1=[indexPath section];
	NSUInteger row1=[indexPath row];
	
	NSString *key=[self.keys objectAtIndex:section1];
	NSArray *array=[tableData objectForKey:key];
	NSRange range=[[self.comments text] rangeOfString:[array objectAtIndex:row1]];
	if (range.location != NSNotFound) {
		return;
	}
	
	NSString *option=@"";
	option=[option stringByAppendingString:[self.comments text]];
	option=[option stringByAppendingString:[array objectAtIndex:row1]];
	option=[option stringByAppendingString:@","];
	self.comments.text=option;
}

- (IBAction)backToOderSummary:(id)sender{
	NSLog(@"backToOderSummary");
	ContextManager *contextMgr=[ContextManager getInstance];
	[contextMgr setGlobalOption:self.comments.text];
	[self.view removeFromSuperview]; 
}

@end
